package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.customer.CustomerConsultationSheet;
import com.probox.common.entity.domain.customer.CustomerInfo;
import com.probox.common.entity.domain.platfom.PlatformStoreGroup;
import com.probox.common.entity.mapper.customer.CustomerConsultationSheetMapper;
import com.probox.common.entity.mapper.system.SysUserMapper;
import com.probox.common.entity.service.customer.ICustomerConsultationSheetService;
import com.probox.common.entity.service.customer.ICustomerInfoService;
import com.probox.common.entity.service.platfom.IPlatformStoreGroupService;
import com.probox.common.entity.vo.customerService.CustomerConsultationSheetVo;
import com.probox.systemApi.domain.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 医美门店新客咨询单Service业务层处理
 *
 * @author usoft
 * @date 2022-09-30
 */
@Service
@Transactional
public class CustomerConsultationSheetServiceImpl extends ServiceImpl<CustomerConsultationSheetMapper, CustomerConsultationSheet> implements ICustomerConsultationSheetService {
    @Autowired
    private CustomerConsultationSheetMapper customerConsultationSheetMapper;

    @Autowired
    private ICustomerInfoService customerInfoService;
    @Autowired
    private IPlatformStoreGroupService platformStoreGroupService;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 新客咨询单
     *
     * @param vo
     * @return
     */
    @Override
    public List<CustomerConsultationSheetVo> listSheet(CustomerConsultationSheetVo vo) {
        /**
         *
         */
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        if (ObjectUtil.isNotEmpty(sysUser.getStoreId()) && ObjectUtil.isEmpty(sysUser.getStoreGroupId())
        ) {
            vo.setStoreId(sysUser.getStoreId());
        } else if (ObjectUtil.isNotEmpty(sysUser.getStoreGroupId())) {
            PlatformStoreGroup byId = platformStoreGroupService.getById(sysUser.getStoreGroupId());
            if (ObjectUtil.isNotEmpty(byId)) {
                if (ObjectUtil.isNotEmpty(byId.getStoreIds())) {
                    vo.setStoreIds(new ArrayList<>(Arrays.asList(byId.getStoreIds().split(","))));
                }
            }
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        return customerConsultationSheetMapper.listSheet(vo);
    }


    @Override
    public CustomerConsultationSheetVo getViewVo(Long sheetId) {
        CustomerConsultationSheet sheet = this.getById(sheetId);
        if (ObjectUtil.isEmpty(sheet)) throw new BaseException("数据不存在");
        CustomerConsultationSheetVo vo = new CustomerConsultationSheetVo();
        BeanUtils.copyProperties(sheet, vo);
        if(ObjectUtil.isNotEmpty(vo.getAllergy())){
            vo.setAllergyList( Arrays.asList(vo.getAllergy().split(",")));
        }
        if(ObjectUtil.isNotEmpty(vo.getMedicalHistory())){
            vo.setMedicalHistoryList( Arrays.asList(vo.getMedicalHistory().split(",")));
        }
        if(ObjectUtil.isNotEmpty(vo.getCure3Month())){
            vo.setCure3MonthList( Arrays.asList(vo.getCure3Month().split(",")));
        }
        return vo;
    }


    /**
     * 新增
     *
     * @param vo
     * @return
     */
    @Override
    public boolean addOrEdit(CustomerConsultationSheetVo vo) {
        if (CollectionUtil.isNotEmpty(vo.getAllergyList())) {
            String collect = vo.getAllergyList().stream().map(vs -> {
                return vs + ",";
            }).collect(Collectors.joining());
            vo.setAllergy(collect.substring(0, collect.length() - 1));
        }
        if (CollectionUtil.isNotEmpty(vo.getMedicalHistoryList())) {
            String collect = vo.getMedicalHistoryList().stream().map(vs -> {
                return vs + ",";
            }).collect(Collectors.joining());
            vo.setMedicalHistory(collect.substring(0, collect.length() - 1));
        }
        if (CollectionUtil.isNotEmpty(vo.getCure3MonthList())) {
            String collect = vo.getCure3MonthList().stream().map(vs -> {
                return vs + ",";
            }).collect(Collectors.joining());
            vo.setCure3Month(collect.substring(0, collect.length() - 1));
        }
        if (ObjectUtil.isEmpty(vo.getSheetId())) {
            CustomerInfo customerInfo = customerInfoService.getById(vo.getCustomerId());
            vo.setCustomerName(customerInfo.getCustomerName());
            vo.setCustomerSex(customerInfo.getSex());
            vo.setPhone(customerInfo.getPhone());
            vo.setAddress(customerInfo.getProvince() + customerInfo.getCity() + customerInfo.getArea());
            vo.setCreateName(SecurityUtils.getUsername());
            vo.setCreateTime(DateUtil.date());
            vo.setCreateUser(SecurityUtils.getUserId());
            vo.setDataStatus(DataStatus.valid.getKey());
            vo.setStoreId(SecurityUtils.getStoreId());
            return this.save(vo);
        } else {
            vo.setUpdateName(SecurityUtils.getUsername());
            vo.setUpdateTime(DateUtil.date());
            vo.setUpdateUser(SecurityUtils.getUserId());
            return this.updateById(vo);
        }
    }


    /**
     * 查询医美门店新客咨询单
     *
     * @param sheetId 医美门店新客咨询单ID
     * @return 医美门店新客咨询单
     */
    @Override
    public CustomerConsultationSheet selectCustomerConsultationSheetById(Long sheetId) {
        return this.getById(sheetId);
    }

    /**
     * 查询医美门店新客咨询单列表
     *
     * @param customerConsultationSheet 医美门店新客咨询单
     * @return 医美门店新客咨询单
     */
    @Override
    public List<CustomerConsultationSheet> queryList(CustomerConsultationSheet customerConsultationSheet) {
        QueryWrapper<CustomerConsultationSheet> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getSheetId())) && customerConsultationSheet.getSheetId() != null) {
            queryWrapper.eq("sheet_id", customerConsultationSheet.getSheetId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getStoreId())) && customerConsultationSheet.getStoreId() != null) {
            queryWrapper.eq("store_id", customerConsultationSheet.getStoreId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getCustomerId())) && customerConsultationSheet.getCustomerId() != null) {
            queryWrapper.eq("customer_id", customerConsultationSheet.getCustomerId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getCustomerName())) && customerConsultationSheet.getCustomerName() != null) {
            queryWrapper.eq("customer_name", customerConsultationSheet.getCustomerName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getCustomerSex())) && customerConsultationSheet.getCustomerSex() != null) {
            queryWrapper.eq("customer_sex", customerConsultationSheet.getCustomerSex());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getBirthday())) && customerConsultationSheet.getBirthday() != null) {
            queryWrapper.eq("birthday", customerConsultationSheet.getBirthday());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getAge())) && customerConsultationSheet.getAge() != null) {
            queryWrapper.eq("age", customerConsultationSheet.getAge());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getPhone())) && customerConsultationSheet.getPhone() != null) {
            queryWrapper.eq("phone", customerConsultationSheet.getPhone());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getAddress())) && customerConsultationSheet.getAddress() != null) {
            queryWrapper.eq("address", customerConsultationSheet.getAddress());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getQudao())) && customerConsultationSheet.getQudao() != null) {
            queryWrapper.eq("qudao", customerConsultationSheet.getQudao());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getContactType())) && customerConsultationSheet.getContactType() != null) {
            queryWrapper.eq("contact_type", customerConsultationSheet.getContactType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getSleepInfo())) && customerConsultationSheet.getSleepInfo() != null) {
            queryWrapper.eq("sleep_info", customerConsultationSheet.getSleepInfo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getAllergy())) && customerConsultationSheet.getAllergy() != null) {
            queryWrapper.eq("allergy", customerConsultationSheet.getAllergy());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getMedicalHistory())) && customerConsultationSheet.getMedicalHistory() != null) {
            queryWrapper.eq("medical_history", customerConsultationSheet.getMedicalHistory());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getCure3Month())) && customerConsultationSheet.getCure3Month() != null) {
            queryWrapper.eq("cure_3_month", customerConsultationSheet.getCure3Month());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getFaceItem())) && customerConsultationSheet.getFaceItem() != null) {
            queryWrapper.eq("face_item", customerConsultationSheet.getFaceItem());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getBodyItem())) && customerConsultationSheet.getBodyItem() != null) {
            queryWrapper.eq("body_item", customerConsultationSheet.getBodyItem());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getDataStatus())) && customerConsultationSheet.getDataStatus() != null) {
            queryWrapper.eq("data_status", customerConsultationSheet.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getCreateUser())) && customerConsultationSheet.getCreateUser() != null) {
            queryWrapper.eq("create_user", customerConsultationSheet.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getCreateName())) && customerConsultationSheet.getCreateName() != null) {
            queryWrapper.eq("create_name", customerConsultationSheet.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getUpdateUser())) && customerConsultationSheet.getUpdateUser() != null) {
            queryWrapper.eq("update_user", customerConsultationSheet.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerConsultationSheet.getUpdateName())) && customerConsultationSheet.getUpdateName() != null) {
            queryWrapper.eq("update_name", customerConsultationSheet.getUpdateName());
        }
        return customerConsultationSheetMapper.selectList(queryWrapper);
    }

    /**
     * 新增医美门店新客咨询单
     *
     * @param customerConsultationSheet 医美门店新客咨询单
     * @return 结果
     */
    @Override
    public boolean insertCustomerConsultationSheet(CustomerConsultationSheet customerConsultationSheet) {

        return this.save(customerConsultationSheet);

    }

    /**
     * 修改医美门店新客咨询单
     *
     * @param customerConsultationSheet 医美门店新客咨询单
     * @return 结果
     */
    @Override
    public boolean updateCustomerConsultationSheet(CustomerConsultationSheet customerConsultationSheet) {

        return this.updateById(customerConsultationSheet);
    }

    /**
     * 批量删除医美门店新客咨询单
     *
     * @param sheetIds 需要删除的医美门店新客咨询单ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerConsultationSheetByIds(Long[] sheetIds) {
        return this.removeByIds(Arrays.asList(sheetIds));
    }


}
